#include <stdio.h>
#include <iostream>
#include "windows.h"

int transfer ()
{
    FILE *comport;

    if ((comport = fopen("COM1", "r+")) == NULL)
    {
        printf("Failed to open the communication port COM2\n");
        printf("The port may be disabled or in use\n");
        return 1;
    }
    printf("COM2 opended successfully\n");



    std::cout<<std::endl<<"1:"<<std::endl;

    fputc( 'b',comport);
    fflush(comport);

    int b = 20;    
    while ( --b ) 
    {
        int a = fgetc(comport);
        if (a != EOF)
            std::cout << (char)a;
    }

    Sleep(7000);

    std::cout<<std::endl<<"2:"<<std::endl;

    fputc( 'c',comport);
    fflush(comport);

    b = 10000;
    while ( --b ) 
    {
        int a = fgetc(comport);
        if (a != EOF)
            std::cout << (char)a;
    }

    //while ( --b ) 
    //{
    //    int a = fgetc(comport);
    //    if (a != EOF)
    //        std::cout << std::hex << a << " ";
    //}


    /*int i = 256;
    while ( --i )
    {
        std::cout << std::dec << std::endl << (char)i << std::endl;
        fputc( (char)i, comport);
        Sleep(100);
        {
            int a;
            int b = 100;

            while ( --b ) 
            {
                a = fgetc(comport);
                if (a != EOF)
                    std::cout << std::hex << a << " ";
            }

        }
    }*/


    //int a;

    //while ( a = fgetc(comport) ) 
    //{
    //    if (a != EOF)
    //        std::cout << std::hex << a << " ";
    //}

   // fflush(comport);
    fclose(comport);
    return 0;
}

int main ()
{
    transfer();
    return 0;
}

